{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m\u001b[32minside  points # 2490\u001b[0m\n",
      "\u001b[1m\u001b[31moutside points # 2510\u001b[0m\n",
      "\u001b[1masphericity: 0.5164696616449839\u001b[0m\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ffad876f8fae466cb6758c840c7ee319",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Plot(antialias=True, axes=['x', 'y', 'z'], axes_helper=1.0, axes_helper_colors=[16711680, 65280, 255], backgro…"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "Draw the PCA (Principal Component Analysis) ellipsoid that contains\n",
    "50% of a cloud of a pointcloud, then check how many points are inside the surface.\n",
    "\"\"\"\n",
    "from vedo import *\n",
    "import numpy as np\n",
    "\n",
    "settings.default_backend = 'k3d'  # or k3d, ipyvtk, trame, or vtk\n",
    "\n",
    "plt = Plotter(size=(1000,500))\n",
    "\n",
    "pts = np.random.randn(5000, 3) * [3,2,1] # random gaussian point cloud\n",
    "\n",
    "elli = pca_ellipsoid(pts, pvalue=0.5)    # group of [ellipse, 3 axes]\n",
    "plt += elli\n",
    "\n",
    "ipts = elli.inside_points(pts)           # extract points inside mesh\n",
    "opts = elli.inside_points(pts, invert=True)\n",
    "plt += Points(ipts, c=\"g\")\n",
    "plt += Points(opts, c=\"r\")\n",
    "\n",
    "printc(\"inside  points #\", ipts.npoints, c='g')\n",
    "printc(\"outside points #\", opts.npoints, c='r')\n",
    "printc(\"asphericity:\", elli.asphericity())\n",
    "plt.show(axes=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<vedo.plotter.Plotter at 0x7403535c0ec0>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[7m\u001b[1mvedo version      : 2025.5.3+dev04  (https://vedo.embl.es)       \u001b[0m\n",
      "\u001b[1mvtk version       : 9.3.1\u001b[0m\n",
      "\u001b[1mnumpy version     : 2.1.3\u001b[0m\n",
      "\u001b[1mpython version    : 3.12.3 (main, Jan 17 2025, 18:03:48) [GCC 13.3.0]\u001b[0m\n",
      "\u001b[1mpython interpreter: /home/musy/vedoenv/bin/python3\u001b[0m\n",
      "\u001b[1minstallation point: /home/musy/Projects/vedo\u001b[0m\n",
      "\u001b[1msystem            : Linux 6.11.0-17-generic posix x86_64\u001b[0m\n",
      "\u001b[2mk3d version       : 2.16.1\u001b[0m\n",
      "\u001b[1m\u001b[33m💡 No input files? Try:\n",
      " vedo https://vedo.embl.es/examples/data/panther.stl.gz\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "!vedo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
